package com.webgis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.webgis.pojo.Szry;
import com.webgis.service.SzryService;

import jakarta.servlet.http.HttpServletResponse;

@RestController
public class WFSController {
    @Autowired
    private SzryService szryService;

    @GetMapping(value = "/wfs", produces = "application/xml; charset=UTF-8")
    public String handleWFSRequest(
            @RequestParam(value = "request", required = false) String request,
            @RequestParam(value = "REQUEST", required = false) String request1,
            @RequestParam(value = "service", required = false) String service,
            @RequestParam(value = "SERVICE", required = false) String service1,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "VERSION", required = false) String version1,
            @RequestParam(value = "typeName", required = false) String typeName,
            HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/xml; charset=UTF-8");

        String actualRequest = request != null ? request : request1;

        try {
            switch (actualRequest != null ? actualRequest.toLowerCase() : "") {
                case "getcapabilities":
                    return szryService.generateGetCapabilitiesResponse();
                case "describefeaturetype":
                    return szryService.generateDescribeFeatureTypeResponse();
                case "getfeature":
                    List<Szry> allPersons = szryService.getAllPersons();
                    return szryService.generateGetFeatureResponse(allPersons);
                default:
                    return createExceptionResponse("OperationNotSupported", "request", "不支持的请求类型: " + actualRequest);
            }
        } catch (Exception e) {
            return createExceptionResponse("InternalError", null, "服务器内部错误: " + e.getMessage());
        }
    }

    private String createExceptionResponse(String exceptionCode, String locator, String message) {
        org.dom4j.Document document = org.dom4j.DocumentHelper.createDocument();
        org.dom4j.Element root = document.addElement("ows:ExceptionReport")
                .addNamespace("ows", "http://www.opengis.net/ows")
                .addAttribute("version", "1.0.0")
                .addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
                .addAttribute("xsi:schemaLocation",
                        "http://www.opengis.net/ows http://schemas.opengis.net/ows/1.0.0/owsExceptionReport.xsd");

        org.dom4j.Element exception = root.addElement("ows:Exception")
                .addAttribute("exceptionCode", exceptionCode);

        if (locator != null) {
            exception.addAttribute("locator", locator);
        }

        exception.addElement("ows:ExceptionText").addText(message);

        return document.asXML();
    }
}
